﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using Yyyg.Models;

namespace Yyyg.WebUI
{
    public static class ModelExt
    {
        private static string rootdir = ConfigurationManager.AppSettings["GoodImageRootDir"];

        /// <summary>
        /// 返回商品主图片的相对路径
        /// </summary>
        public static string GetMainImgPath(this Goods g)
        {
            return rootdir + "/" + g.Guid + "/" + g.Mainimg_url;
        }

        /// <summary>
        /// 返回商品附属图片的相对路径集
        /// <summary>
        public static string[] GetSubImgsPath(this Goods g)
        {
            string[] ns = JsonConvert.DeserializeObject<string[]>(g.Subimgs_url);
            string[] ps = new string[ns.Length];
            for (int i = 0; i < ns.Length; i++)
            {
                ps[i] = rootdir + "/" + g.Guid + "/" + ns[i];
            }
            return ps;
        }

        /// <summary>
        /// 返回商品详情图片的相对路径集
        /// <summary>
        public static string[] GetDetImgsPath(this Goods g)
        {
            string[] ns = JsonConvert.DeserializeObject<string[]>(g.Detimgs_url);
            string[] ps = new string[ns.Length];
            for (int i = 0; i < ns.Length; i++)
            {
                ps[i] = rootdir + "/" + g.Guid + "/" + ns[i];
            }
            return ps;
        }


        /// <summary>
        /// 返回当前夺宝进度百分比
        /// </summary>
        /// <param name="rush"></param>
        /// <returns></returns>
        public static double NowPercentage(this Rushby_all rush)
        {
            return Math.Round(((double)rush.All_told - (double)rush.Rem) / (double)rush.All_told * 100, 2);
        }



        /// <summary>
        /// 返回北京时间格式
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ToBeijingString(this DateTime time)
        {
            return time.ToString("yyyy年MM月dd日 HH:mm:ss");
        }

        /// <summary>
        /// sql查询所有指定键包含在序列中的数据
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="field">字段名</param>
        /// <param name="ins">对比序列</param>
        /// <returns></returns>
        public static DbSqlQuery<TSource> In<TSource>(this DbSet<TSource> ts, string field, IEnumerable<string> ins) where TSource : class
        {
            var entity = ts.FirstOrDefault();
            if (entity == null) return null;
            string tbname = typeof(TSource).Name;
            StringBuilder builder = new StringBuilder();
            var insarr = ins.ToArray();
            for (int i = 0; i < insarr.Length; i++) builder.Append("\'" + insarr[i] + "\',");
            var sql = $"select * from {tbname} where {field} in ( {builder.ToString().TrimEnd(',')} )";
            return ts.SqlQuery(sql);
        }

        /// <summary>
        /// sql查询所有指定键包含在序列中的数据
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="field">字段名</param>
        /// <param name="ins">对比序列</param>
        public static DbSqlQuery<TSource> In<TSource>(this DbSet<TSource> ts, string field, IEnumerable<int> ins) where TSource : class
        {
            var entity = ts.FirstOrDefault();
            if (entity == null) return null;
            string tbname = typeof(TSource).Name;
            StringBuilder builder = new StringBuilder();
            var insarr = ins.ToArray();
            for (int i = 0; i < insarr.Length; i++) builder.Append(insarr[i] + ",");
            var sql = $"select * from {tbname} where {field} in ( {builder.ToString().TrimEnd(',')} )";
            return ts.SqlQuery(sql);
        }
    }
}